package com.anytrek.cloudorder.controller;

import com.anytrek.cloudcommon.utils.ApiResult;
import com.anytrek.cloudorder.service.OrdersService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * @ ClassName OrdersController
 * @ Author Alex2
 * @ Date 2025/8/22 12:23
 **/
@RestController
@RequestMapping("/orders")
@RequiredArgsConstructor
public class OrdersController {

    private final OrdersService ordersService;

    // http://localhost:8882/orders/getOrderByUserId?userId=1
    @GetMapping("/getOrderByUserId")
    public ApiResult getOrderByUserId(@RequestParam(value = "userId") Integer userId,
                                      @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                      @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        return ordersService.getOrderByUserId(userId, pageNum, pageSize);
    }

    // http://localhost:8882/orders/getOrderById?id=1
    @GetMapping("/getOrderById")
    public ApiResult getOrderById(@RequestParam(value = "id") Integer id) {
        return ordersService.getOrderById(id);
    }

    // http://localhost:8882/orders/payment
    @GetMapping("/payment")
    public ApiResult payment() {
        return ordersService.payment();
    }

    // http://localhost:8882/orders/updateOrderById?id=1&name=knife&price=4&userId=4
    @GetMapping("updateOrderById")
    public ApiResult updateOrderById(@RequestParam(value = "id") Integer id,
                                     @RequestParam(value = "name") String name,
                                     @RequestParam(value = "price") Integer price,
                                     @RequestParam(value = "userId") Integer userId) {
        ordersService.getUserById(id);
        return ordersService.updateOrderById(id, name, price, userId);
    }

    // http://localhost:8882/orders/deleteOrderById?id=1
    @GetMapping("deleteOrderById")
    public ApiResult deleteOrderById(@RequestParam(value = "id") Integer id) {
        ordersService.getUserById(id);
        return ordersService.deleteOrderById(id);
    }

    /**
     * @ Author: Alex
     * @ Description: 项目实战-分布式事务
     */
    // http://localhost:8882/orders/insertOrder?commodityId=1&count=20&userId=4
    @GetMapping("insertOrder")
    public ApiResult insertOrder(@RequestParam(value = "commodityId") Integer commodityId,
                                 @RequestParam(value = "count") Integer count,
                                 @RequestParam(value = "userId") Integer userId) {
        return ordersService.insertOrder(commodityId, count, userId);
    }
}